package com.itheima.reggie.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    //套餐列表
    @GetMapping("/setmeal/page")
    public ResultInfo findByPage(@RequestParam(value = "page",defaultValue = "1")Integer pageNum,
                                 @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                 String name){
        Page<Setmeal> page = setmealService.findByPage(pageNum, pageSize,name);
        return ResultInfo.success(page);
    }
    //新增套餐
    @PostMapping("/setmeal")
    public ResultInfo save(@RequestBody Setmeal setmeal){
        setmealService.save(setmeal);
        return ResultInfo.success(null);
    }
    //修改套餐回显
    @GetMapping("/setmeal/{id}")
    public ResultInfo findById(@PathVariable Long id){
        Setmeal setmeal = setmealService.findById(id);
        return ResultInfo.success(setmeal);
    }
    //修改套餐
    @PutMapping("/setmeal")
    public ResultInfo update(@RequestBody Setmeal setmeal){
        setmealService.update(setmeal);
        return ResultInfo.success(null);
    }
    //批量删除、删除
    @DeleteMapping("/setmeal")
    public ResultInfo delete(@RequestParam List<Long> ids){
        if (CollectionUtil.isEmpty(ids)) {
            throw new CustomException("请勾选要删除的套餐");
        }
        setmealService.delete(ids);
        return ResultInfo.success(null);
    }
    //批量启售、禁售
    @PostMapping("/setmeal/status/{status}")
    public ResultInfo updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        if (CollectionUtil.isEmpty(ids)) {
            throw new CustomException("请先勾选套餐");
        }
        setmealService.updateStatus(status,ids);
        return ResultInfo.success(null);
    }

}
